
<!--互联网售票机数据录入-->
<template>
  <div class="huLianWangShouPiaojiDataReal">
    <basic-container>
      <avue-crud
        ref="huLianWangShouPiaojiDataReal"
        :option="option"
        :table-loading="listLoading"
        :data="list"
        :page.sync="page"
        @search-change="handleFilter"
        @selection-change="selectionChange"
        @refresh-change="refreshChange"
        @on-load="getList">
        <template slot-scope="scope" slot="menuRight">
          <el-button type="primary"  size="mini" @click="shenpiTongGuo()">审批通过</el-button>
          <el-button type="primary" size="mini" @click="submitData()">加入报表差错</el-button>
          <el-button type="primary"  size="mini" v-if="false" @click="replaceContent">一键替换</el-button>
          <Export style="display: inline-flex;padding: 0px;width: auto" @importSuccess="getList"></Export>
          <el-button type="primary"  size="mini" @click="xcExport">协查导出</el-button>
          <el-button type="primary"  size="mini" @click="openExportData('导出（分线路）',true)">导出（分线路）</el-button>
          <el-button type="primary"  size="mini" @click="openExportData('导出（分日期）',false)">导出（分日期）</el-button>
        </template>
        <template slot-scope="scope" slot="lineCodeSearch">
          <el-select v-model="searchForm.lineCode" placeholder="请选择" style="width: 150px" @change="changeLine" clearable>
            <el-option
              v-for="(item,index) in lineCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="stationCodeSearch">
          <el-select v-model="searchForm.stationCode" placeholder="请选择" style="width: 150px" @change="changeStation" clearable>
            <el-option
              v-for="(item,index) in stationCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        
        <!-- slot卡槽的名字为prop+Search，对应下面option的column里面的prop -->
        <template slot-scope="scope" slot="equipmentCodeSearch">
          <el-select v-model="searchForm.equipmentCode" placeholder="请选择" @change="getSheBei" style="width: 150px" clearable>
            <el-option
              v-for="(item,index) in equipmentNumbers"
              :key="index"
              :label="item.equipmentCode"
              :value="item.equipmentCode">
            </el-option>
          </el-select>
        </template>

        <template slot="search" slot-scope="{row,size}" >
          <el-form-item label="差异为零" style="width: 300px">
            <el-select v-model="searchForm.isDiff" placeholder="请选择">
              <el-option label="否" value="0"></el-option>
              <el-option label="是" value="1"></el-option>
            </el-select>
          </el-form-item>
        </template>

        <template slot="searchMenu" slot-scope="{row,size}">
          <el-button :size="size" @click="()=>{setSearchFiledsEmpty(row);setSearchFiledsEmpty(searchForm);getList()}" icon="el-icon-delete">清空</el-button>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button type="text" size="small" v-if="scope.row.approvalStatus==0" plain @click.stop="editor(scope.row)">编辑</el-button>
          <el-button type="text" size="small" plain @click.stop="preview(scope.row)">预览</el-button>
        </template>
      </avue-crud>
    </basic-container>
    <EditorOrAdd ref="EditorOrAdd" v-if="isShowDia"></EditorOrAdd>
    <addReportEditor ref="addReportEditor" @addReportSuccess="getList"></addReportEditor>
<!--    弹框-->
    <div class="dilog export">
      <el-dialog
        :title="exportDia.title"
        :visible.sync="exportDia.dialogVisible"
        width="800px"
        @close="exportDia.dialogVisible=false"
      >
        <span>开始日期：</span>
        <el-date-picker
        v-model="exportDia.startTime"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择日期">
      </el-date-picker>

        <span style="margin-left: 20px">结束日期：</span>
        <el-date-picker
        v-model="exportDia.endTime"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择日期">
      </el-date-picker>
        <br/> <br/>
        <div v-if="exportDia.showLine">
        <span style="margin-left: 20px">线路：</span>
        <el-select v-model="exportDia.lineCode" multiple placeholder="请选择" clearable >
          <el-option
            v-for="(item,index) in exportDia.lineCodes"
            :key="index"
            :label="getLineName(item.lineCode)"
            :value="item.lineCode">
          </el-option>
        </el-select>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="exportWith()">确 定</el-button>
          <el-button @click="exportDia.dialogVisible = false">取 消</el-button>
        </span>
      </el-dialog>

    </div>
  </div>

</template>

<script>
  import {getForm, postForm} from '@/api/gen/gen';
  import Export from './Export';
  import {mapGetters} from 'vuex';
  import EditorOrAdd from './EditorOrAdd';
  import {tableOption,getSwAuInputInternetTicketEquipmentList} from '../../../../../../api/subwayRat/TVMRevenueVarianceAudit.TvmChaYiDXC/huLianWangShoupiaoji/huLianWangShouPiaojiDataInput.js';
  import {
    approveStatus, clearSelect,
    getgetStationCodes,getSwPmDeviceOperationCodeList,getSwPmInternetTicketEquipmentCodeList,
    getLineCodes, getLineName, setSearchFiledsEmpty,fenExYuangettoFixed2
  } from "../../../../../../api/subwayRat/commonCheck";
  import {exportDataToExcel} from "../../../../../../api/subwayRat/exportSelfHlw";
  import {
    approvePassSwAuInputInternetTicketEquipment,
    //exportSwAuInputInternetTicketEquipment,
    exportInternetTicketEquipmentDate,
    exportInternetTicketEquipmentLine,
    getIds,
    getLineCodeWithSwAuInputInternetTicketEquipment,
    replaceSwAuInputInternetTicketEquipmentJine,
    xcExportDataSwAuInputInternetTicketEquipments,
    exportInternet
  } from "../../../../../../api/subwayRat/TVMRevenueVarianceAudit.TvmChaYiDXC/huLianWangShoupiaoji/huLianWangShouPiaojiDataInput";
  import fileDownload from 'js-file-download';
  import addReportEditor from '@/views/subwayRat/reportMondify/reportMistakeInfo/Editor';
  import { handleExcelResponse } from "@/util/util";
  export default {
    name: 'huLianWangShouPiaojiDataInput',
    components:{
      EditorOrAdd,Export,addReportEditor
    },
    data() {
      return {
        exportDia:{
          title:'',
          dialogVisible:false,
          startTime:'',
          endTime:'',
          lineCodes:[],
          showLine:false
        },
        isShowDia:false,
        option: {},
        listLoading: true,
        lineCodes:[],
        stationCodes:[],
        selectKeys:[],
        selectKeyVals:[],
        searchForm:{
          lineCode:'',
          stationCode:'',
          isDiff:''
        },
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20, // 每页显示多少条,
          isAsc: false// 是否倒序
        },
        list : [],
        equipmentNumbers:[],
      }
    },
    computed: {
      ...mapGetters(['permissions']),
    },
    watch: {
    },
    created() {
      this.option=tableOption;
    },
    methods: {
      async submitData() {
        if (this.selectKeys.length != 1) {
          this.$message.warning("请选择一条要加入的数据！");
          return;
        }
        for (let i = 0; i < this.selectKeys.length; i++) {
          if (this.selectKeys[i].auditStatus == 1) {
            this.$message.warning("请选择未审核的数据！");
            return;
          }
        }
        let res = await this.$confirm('是否提交报表差错？', '提示', {
          confirmButtonText: '是',
          cancelButtonText: '否',
          type: 'warning'
        });
        if (!res) {
          return;
        }
        let param={
          resultIds:this.selectKeys[0].id,
          operatorTable:"sw_au_internet_ticket_equipment",
          lineCode:this.selectKeys[0].lineCode,
          stationCode:this.selectKeys[0].stationCode,
          reprotTime:this.selectKeys[0].revenueDate,
          reportNameId:13,
          settleBeginTime:this.selectKeys[0].startTime,
          settleEndTime:this.selectKeys[0].endTime,
          userCodes: this.selectKeys[0].creatorName,
          showType:3,
          dataSource:12,
          errorAmount:fenExYuangettoFixed2(this.selectKeys[0].chayije),
          equipmentCode: this.selectKeys[0].equipmentCode
        };
        this.$refs.addReportEditor.others_report_add_call(param);

      },



      setSearchFiledsEmpty,
      getLineName,
      refreshChange() {
        this.getList()
      },
      getList() {
        this.listLoading = true;
        Object.assign(this.searchForm,this.page);
        getSwAuInputInternetTicketEquipmentList(this.searchForm).then((response)=>{
          this.list = response.data.data;
          this.listLoading = false;
          this.page.total=response.data.total;
        });
      },
      async shenpiTongGuo(){ //审批通过
        if(this.selectKeys.length<=0){
           this.$message.warning("未选择数据");
           return;
        }
        for(let i=0;i<this.selectKeys.length;i++){
          if(this.selectKeys[i].approvalStatus==1){
              this.$message.warning("请选择未审批的！");
              return;
          }
        }
        let res = await this.$confirm('是否一键审核？', '提示', {
          confirmButtonText: '是',
          cancelButtonText: '否',
          type: 'warning'
        });
        if (!res) {
          return;
        }

        let param=getIds(this.selectKeys);
        let response = await approvePassSwAuInputInternetTicketEquipment(param);
          if(response.data.code==0){
            this.$message.success("操作成功！");
            this.getList();
          }else{
            this.$message.error("操作失败！");
          }

        clearSelect(this.$refs.huLianWangShouPiaojiDataReal);
      },
      replaceContent(){
        if(this.selectKeys.length<=0){
          this.$message.warning("未选择数据");
          return;
        }
        for(let i=0;i<this.selectKeys.length;i++){
          if(this.selectKeys[i].approvalStatus==1){
            this.$message.warning("请选择未审批的！");
            return;
          }
        }
        replaceSwAuInputInternetTicketEquipmentJine({approvalStatus:1,ids:this.selectKeyVals.join(",")}).then(res=>{
          if(res.data.code==0){
            this.$message.success("操作成功！");
            this.getList();
            clearSelect(this.$refs.huLianWangShouPiaojiDataReal);
          }else{
            this.$message.error("操作失败！");
          }
        }).catch(e=>{
          this.$message.error("操作失败！");
        });
      },
      openExportData(title,showLine){ //打开弹框，并且
        this.exportDia.title=title;
        this.exportDia.dialogVisible=true;
        this.exportDia.showLine=showLine;
        getLineCodeWithSwAuInputInternetTicketEquipment({}).then(res=>{
          if(res.data.code!=0){
            this.$message.error("获取线路失败！");
            return;
          }
          this.exportDia.lineCodes=res.data.data;
        })
      },
      async exportWith(){
        clearSelect(this.$refs.huLianWangShouPiaojiDataReal);
        let p={};
        p.startTime=this.exportDia.startTime;
        p.endTime=this.exportDia.endTime;
        let lineCodes = '';
        if(this.exportDia.lineCode!=null){
          for(let i=0;i<this.exportDia.lineCode.length;i++){
            lineCodes += "," + this.exportDia.lineCode[i];
          }
          if(this.exportDia.showLine){
            p.lineCodes=lineCodes.substring(1);
          }
        }
        if(this.exportDia.showLine){
          let loading = this.$loading({
            lock: true,
            text: "Loading",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)"
          })
          try {
            const res = await exportInternetTicketEquipmentLine(p);
            if(res.data.size==0){
              this.$message.error('所选时间范围内和线路暂无数据导出，请重新选择！')
            }
            handleExcelResponse(res);
          } finally {
            loading.close();
          }

        }else{
          let res=await exportInternetTicketEquipmentDate(p);
          let data = res.data.data;
          exportDataToExcel(data,this.option.column,'互联网售票机对账确认');
        }
      },
      editor(row){
        this.isShowDia=true;
        this.$nextTick(()=>{
          this.$refs.EditorOrAdd.recallShow(null,row,2);
        })
      },
      async xcExport(){ //协查导出
        if(this.selectKeys.length<=0){
          this.$message.warning("未选择数据");
          return;
        }
        let loading = this.$loading({
          lock: true,
          text: "Loading",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)"
        })
        try {
          if(this.selectKeys.length>0){
            const res = await exportInternet({ids:this.selectKeyVals.join(",")});
            handleExcelResponse(res);
          }else{
            const res = await exportInternet(this.searchForm);
            handleExcelResponse(res);
          }
        } finally {
          loading.close();
        }
        // xcExportDataSwAuInputInternetTicketEquipments(this.searchForm).then(res=>{
        //   // let fileDownload = require('js-file-download');
        //     fileDownload(res.data,"互联网售票机协查导出.xlsx");
        // })
      },
      preview(row){
        this.isShowDia=true;
        this.$nextTick(()=>{
          this.$refs.EditorOrAdd.recallShow(null,row,3);
        })
      },
      selectionChange(list){
        this.selectKeys=list;
        this.selectKeyVals=[];
        for(let i=0;i<list.length;i++){
          this.selectKeyVals.push(list[i].id);
        }
      },
      handleFilter(params, done) {
        Object.assign(this.searchForm,params);
        this.getList()
        done()
      },
      async exportData(p){ // 导出
        if(!p){
          p={};
        }
        Object.assign(p,this.searchForm);
        p.pageNo=1;
        p.pageSize=1000000000;
        if(this.selectKeys.length>0){
          exportDataToExcel(this.selectKeys,this.option.column,'互联网售票机数据录入');
        }else {
          let res=await getSwAuInputInternetTicketEquipmentList(p);
          let data = res.data.data;
          exportDataToExcel(data,this.option.column,'互联网售票机数据录入');
        }
      },
      changeLine(v){ //改变线路时
        getgetStationCodes(v).then(res=>{
          this.stationCodes=res;
          this.searchForm.stationCode='';
          this.searchForm.equipmentCode = '';
        })
      },
      changeStation(v){
        this.searchForm.equipmentCode = '';
        this.getSheBei();
      },
      getSheBei(){
          getSwPmInternetTicketEquipmentCodeList({lineCode:this.searchForm.lineCode,stationCode:this.searchForm.stationCode}).then(r=>{
            this.equipmentNumbers=r.data.data;
        });
      },
    },
    mounted() {
      getLineCodes().then(res=>{ //获取线路
        this.lineCodes=res;
      });
    }
  }
</script>
<style lang="scss">
  .huLianWangShouPiaojiDataReal {
    height: 100%;

    &__tree {
      padding-top: 3px;
      padding-right: 20px;
    }

    &__main {
      .el-card__body {
        padding-top: 0;
      }
    }
    .avue-form__group{

    }
  }
</style>
